package com.gooluke.mall.order.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author gooluke
 */
@Configuration
public class ServerConfiguration {

    private final static Logger logger = LoggerFactory.getLogger(ServerConfiguration.class);


    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
        try {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("classpath:/mapper/*.xml");
            Resource config = resolver.getResource("classpath:mybatis-config.xml");
            sqlSessionFactoryBean.setMapperLocations(resources);
            sqlSessionFactoryBean.setConfigLocation(config);
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            logger.error("create sqlSessionFactoryBean error", e);
            throw new RuntimeException("create sqlSessionFactoryBean error");
        }
    }

    @Bean("dataSource")
    public DataSource dataSource(@Value("${jdbc.datasource.driverClassName}") String driverClassName,
                                 @Value("${jdbc.datasource.url}") String url,
                                 @Value("${jdbc.datasource.username}") String username,
                                 @Value("${jdbc.datasource.password}") String password
    ) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }

}
